package com.lion.seaman.common.tools;

import java.math.BigDecimal;

/**
 * Created by liyongyao on 2016/12/20.
 */
public class MathTools {

    /**
     * 相加
     *
     * @param a1
     * @param b1
     * @return
     */
    public static double add(double a1, double b1) {
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));
        return a2.add(b2).doubleValue();
    }

    /**
     * 相减
     *
     * @param a1
     * @param b1
     * @return
     */
    public static double sub(double a1, double b1) {
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));
        return a2.subtract(b2).doubleValue();
    }

    /**
     * 相乘
     *
     * @param a1
     * @param b1
     * @return
     */
    public static double mul(double a1, double b1) {
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));
        return a2.multiply(b2).doubleValue();
    }

    /**
     * 相除
     *
     * @param a1
     * @param b1
     * @param scale
     * @return
     */
    public static double div(double a1, double b1, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("error");

        }
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));
        return a2.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 保留两位小数<br>
     * <b>四舍五入</b>
     *
     * @param d
     * @return
     */
    public static double keep2Point(double d) {
        return bigDecimal(d, 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 保留两位小数<br>
     * <b>进位</b>
     *
     * @param d
     * @return
     */
    public static double keep2PointUp(double d) {
        return bigDecimal(d, 2, BigDecimal.ROUND_UP);
    }

    /**
     * 保留两位小数<br>
     * <b>舍位</b>
     *
     * @param d
     * @return
     */
    public static double keep2PointDown(double d) {
        return bigDecimal(d, 2, BigDecimal.ROUND_DOWN);
    }

    /**
     * 基础方法
     *
     * @param d
     *            原数字
     * @param newScale
     *            保留小数位数
     * @param roundingMode
     *            模式,例如：BigDecimal.ROUND_DOWN
     * @return
     */
    public static double bigDecimal(double d, int newScale, int roundingMode) {
        BigDecimal bigDec = new BigDecimal(d);
        return bigDec.setScale(newScale, roundingMode).doubleValue();
    }

    public static int compareTo(double a1, double b1) {
        BigDecimal a2 = new BigDecimal(Double.toString(a1));
        BigDecimal b2 = new BigDecimal(Double.toString(b1));
        return a2.compareTo(b2);
    }

    public static void main(String[] args) {
        double score = 08d;
        double weight = 09d;
        System.out.printf((MathTools.compareTo(score, weight) >= 0) + "");
        System.out.printf((score >= weight) + "");
    }
}